const { Controller } = require('egg');

class $TABLE_NAME$Controller extends Controller {
    async list() {
        try {
            const { ctx } = this;
            const {
                page = 1,    // 当前页码，默认第1页
                limit = 10   // 每页数量，默认10条
            } = ctx.query;
            const offset = (page - 1) * limit; // 计算偏移量
            const result = await ctx.service.$TABLE_NAME$.list(offset, limit);
            ctx.body = {
                code: 20000,
                data: result
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器list错误'+error
            }
        }

    }

    async get() {
        try {
            const { ctx } = this;
            var $PRIMARYKEY$ = ctx.query.$PRIMARYKEY$;
            if (!$PRIMARYKEY$) {
                ctx.body = {
                    code: 400,
                    msg: '缺少参数'
                }
                return;
            }
            const res = await ctx.service.$TABLE_NAME$.detail($PRIMARYKEY$);
            if (res) {
                ctx.body = {
                    code: 20000,
                    data: res,
                    msg: '查询成功'
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '服务器get错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器get错误'+error
            }
        }

    }

    async add() {
        try {
            const { ctx } = this;
            var objData = ctx.request.body;
            var rlt = await this.ctx.service.$TABLE_NAME$.add(objData);
            if (rlt) {
                this.ctx.body = {
                    code: 20000,
                    data: rlt,
                    msg: '添加成功'
                }
            } else {
                this.ctx.body = {
                    code: 500,
                    msg: '服务器add错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器add错误'+error
            }
        }



    }

    async del() {
        try {
            const { ctx } = this;
            var $PRIMARYKEY$  = ctx.query.$PRIMARYKEY$;
            const res = await ctx.service.$TABLE_NAME$.del($PRIMARYKEY$);
            if (res.code == 400) {
                ctx.body = {
                    code: 400,
                    msg: res.msg
                }
            } else if (res.code == 200) {
                ctx.body = {
                    code: 20000,
                    msg: res.msg
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '服务器del错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器del错误'+error
            }
        }


    }

    async update() {
        try {
            const { ctx } = this;
            var objData = ctx.request.body;
            var rlt = await this.ctx.service.$TABLE_NAME$.update(objData);
            if (rlt) {
                if (rlt.code == 400) {
                    this.ctx.body = {
                        code: 400,
                        msg: rlt.msg
                    }
                    return
                }
                this.ctx.body = {
                    code: 20000,
                    data: rlt,
                    msg: '修改成功'
                }
            } else {
                this.ctx.body = {
                    code: 500,
                    msg: '服务器update错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器update错误'+error
            }
        }


    }
}

module.exports = $TABLE_NAME$Controller;
